package account

import (
	"github.com/gin-gonic/gin"
	"hobby-mate/common"
	accountService "hobby-mate/service/account"
	"hobby-mate/util"
)

// Controller 账户控制器
type Controller struct {
	util.HoldContext
}

// CreateAccountApi 账户新增
// @Summary 账户新增
// @tags Account
// @Router /sys/token/account/_add-account [post]
// @Produce application/json
// @Param data body CreateAccountVo true "账户信息"
// @Success 200 {string} json "{"code":200,"msg":"create!","data":null}"
// @Failure 500 {string} json "{"code":500,"msg":"add account failed","data":null}"
func (account Controller) CreateAccountApi(routerService *gin.Context) {
	createAccountVo := &CreateAccountVo{}
	err := account.
		MakeContext(routerService).
		BindVoJSON(createAccountVo).
		Errors
	if err != nil {
		return
	}
	//校验参数
	if util.StrIsEmpty(createAccountVo.Username) || util.StrIsEmpty(createAccountVo.Password) {
		common.ReturnError(routerService, "add account failed,username or password is empty")
		return
	}
	createAccountDTO := &accountService.CreateAccountDTO{
		BaseDTO: accountService.BaseDTO{
			Username: createAccountVo.Username,
			Password: createAccountVo.Password,
		},
	}
	if _, err := accountService.CreateAccount(createAccountDTO); err != nil {
		common.ReturnError(routerService, "add account failed"+err.Error())
		return
	}
	common.ReturnOkSuccess(routerService, nil)
}

// LoginApi 账户登录
// @Summary 账户登录
// @tags Account
// @Router /sys/open/account/_login [post]
// @Produce application/json
// @Param data body LoginVo true "账户信息"
// @Success 200 {string} json "{"code":200,"msg":"create!","data":null}"
// @Failure 500 {string} json "{"code":500,"msg":"login failed","data":null}"
func (account Controller) LoginApi(routerService *gin.Context) {
	loginVo := &LoginVo{}
	err := account.
		MakeContext(routerService).
		BindVoJSON(loginVo).
		Errors
	if err != nil {
		common.ReturnError(routerService, "login failed "+err.Error())
		return
	}
	base := accountService.BaseDTO{
		Username: loginVo.Username,
		Password: loginVo.Password,
	}
	token, serviceErr := accountService.Login(&(accountService.LoginDTO{BaseDTO: base}))
	if serviceErr != nil {
		common.ReturnError(routerService, "login failed "+serviceErr.Error())
		routerService.Set("login_result", false)
		return
	}
	routerService.Set("login_result", true)
	routerService.Set("data", token)
}

// GetAllAccountByPageApi 获取所有账户信息
// @Summary 获取所有账户信息
// @tags Account
// @Router /sys/token/account/_get-all-account-by-page [post]
// @Produce application/json
// @Param data body common.PageQuery true "账户信息"
// @Success 200 {string} json "{"code":200,"msg":"create!","data":null}"
// @Failure 500 {string} json "{"code":500,"msg":"query failed","data":null}"
func (account Controller) GetAllAccountByPageApi(routerService *gin.Context) {
	//获取所有账户信息进行分页
	pageQuery := &common.PageQuery{}
	err := account.
		MakeContext(routerService).
		BindVoJSON(pageQuery).
		Errors
	if err != nil {
		common.ReturnError(routerService, "query failed "+err.Error())
		return
	}
	result, err := accountService.QueryUserByCondition(pageQuery)
	if err != nil {
		common.ReturnError(routerService, "query failed "+err.Error())
	}
	common.ReturnOkSuccess(routerService, result)
}
